package com.example.learn.controller;

import com.example.learn.pojo.Daily;
import com.example.learn.service.DailyService;
import com.example.learn.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/daily")
public class DailyController {

    @Autowired
    DailyService dailyService;

    @GetMapping("/list")
    public R<Map> getProductList() {
        List<Daily> dailyList = dailyService.getDailyList();
        Map dataMap = new HashMap();
        dataMap.put("data", dailyList);
        dataMap.put("total", dailyList.size());
        return R.success(dataMap,"success");
    }

    @PostMapping("/add")
    public R addDaily(@RequestBody Daily daily) {
        int count = dailyService.addDaily(daily);
        if(count >= 1){
            return R.success("添加成功");
        }else {
            return R.failed("添加失败");
        }
    }

    @GetMapping("/delete")
    public R delDailyById(String id) {
        int count = dailyService.delDailyById(id);
        if(count >= 1){
            return R.success("删除成功");
        }else {
            return R.failed("删除失败");
        }
    }
}
